define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'adminlte', 'template'], function ($, undefined, Backend, Table, Form, Adminlte, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'student/index',
                    add_url: 'student/add',
                    edit_url: 'student/edit',
                    del_url: 'student/del',
                    multi_url: 'student/multi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {field: '0', checkbox: true,},
                        {
                            field: 'class_id', title: __('学校/年级/班级'), searchList: function (column) {
                                return Template('schoolTpl', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'number', title: __('Number'), operate: 'LIKE %...%',},
                        {field: 'edu_id_number', title: __('Edu Id Number'), operate: 'LIKE %...%',},
                        {field: 'card_number', title: __('Card Number'), operate: 'LIKE %...%',},
                        {
                            field: 'avatar',
                            title: __('Avatar'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%',},
                        {field: 'gender_text', title: __('Gender'), operate: false},
                        {field: 'birthday', title: __('Birthday'), operate: false},
                        {field: 'school.name', title: __('School'), operate: false,},
                        {field: 'grade.name', title: __('Grade'), operate: false,},
                        {field: 'classes.name', title: __('Classes'), operate: false,},
                        {field: 'parents_name', title: __('Parents Name'), operate: false,},
                        {field: 'parents_mobile', title: __('Parents Mobile'), operate: false,},
                        {field: 'qrcode', title: __('二维码'), operate: false, formatter: Controller.api.formatter.qrcode},
                        {
                            field: 'created_at',
                            title: __('Create time'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            align: 'center',
                            formatter: Table.api.formatter.toggle,
                            operate: false
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                commonSearch: true,
                searchFormVisible: true,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on('click', '.btn-import', function () {
                Backend.api.open('student/import', '导入');
            });

            $(document).delegate(".qrcode", "click", function () {
                Backend.api.open('', '二维码', {
                    type: 1,
                    title: false,
                    closeBtn: 1,
                    area: '516px',
                    skin: 'layui-layer-nobg', //没有背景色
                    shadeClose: true,
                    content: $('#qrcode_' + $(this).data('id'))
                });
            });

        },
        add: function () {

            // $(document).on('change', '#c-school', function () {
            //     $('#tpl_div').html(Template('tpl'));
            //
            //     $('#c-grade').selectPage({
            //         showField: 'name',
            //         keyField: 'id',
            //         data: 'grade/selectpage',
            //         eAjaxSuccess: function (data) {
            //             data.list = typeof data.rows !== 'undefined' ? data.rows : (typeof data.list !== 'undefined' ? data.list : []);
            //             data.totalRow = typeof data.total !== 'undefined' ? data.total : (typeof data.totalRow !== 'undefined' ? data.totalRow : data.list.length);
            //             return data;
            //         },
            //         params: function(){
            //             return {'school_id': $('#c-school').val()};
            //         }
            //     });
            // });
            //
            // $(document).on('change', '#c-grade', function () {
            //     $('#c-classes').selectPage({
            //         showField: 'name',
            //         keyField: 'id',
            //         data: 'classes/selectpage',
            //         eAjaxSuccess: function (data) {
            //             data.list = typeof data.rows !== 'undefined' ? data.rows : (typeof data.list !== 'undefined' ? data.list : []);
            //             data.totalRow = typeof data.total !== 'undefined' ? data.total : (typeof data.totalRow !== 'undefined' ? data.totalRow : data.list.length);
            //             return data;
            //         },
            //         params: function(){
            //             return {'grade_id': $('#c-grade').val()};
            //         }
            //     });
            // });




            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        import: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                Form.events.datetimepicker($("form[role=form]"));
                //Form.events.selectpage($("form[role=form]"));
            },
            formatter: {
                qrcode: function (value, row, index) {
                    return "<img id='qrcode_" + row.id + "' style='width:100%;display:none;' src='" + row.qrcode + "'><a class='qrcode' href='javascript:;' style='max-width: 50px; max-height: 50px; display:inline-block;' data-id='" + row.id + "'><img style='width:100%;' src='" + row.qrcode + "'></a>";
                }
            },
        }
    };
    return Controller;
});